//
//  HapkidoFreestylePresController.swift
//  I.S.A.P WT
//
//  Created by Илья Проскурнев on 24.05.2021.
//  Copyright © 2021 Илья Проскурнев. All rights reserved.
//

import Foundation
import UIKit

class HapkidoFreestylePresController : UIViewController, UINavigationControllerDelegate, RadioButtonFreestylePres1Delegate, RadioButtonFreestylePres2Delegate, RadioButtonFreestylePres3Delegate, RadioButtonFreestylePres4Delegate {
    
    @IBOutlet weak var accyracy: UILabel!
    @IBOutlet weak var presentation: UILabel!
    @IBOutlet weak var total: UILabel!
    
    @IBOutlet weak var send: UIButton!
    
    var radioButtonFreestylePres1: RadioButtonFreestylePres1?
    var radioButtonFreestylePres2: RadioButtonFreestylePres2?
    var radioButtonFreestylePres3: RadioButtonFreestylePres3?
    var radioButtonFreestylePres4: RadioButtonFreestylePres4?
    
    var Acd: Double!
    var num01: Int!
    var num03: Int!
    
    var freestyleViv: Int!
    
    var pr1 = 0
    var pr2 = 0
    var pr3 = 0
    var pr4 = 0
    
    var idvistuplenie, success: Int!
    var idhost: String!
    var presentationOc: Int!
    var pokaz: Int!
    var vistocen: Int!
    var kort: Int?
    var sys: Int!
    var sudNum: Int!
    var kolSud: Int!
    let URL_OCEN2 = "/php_isap/ocen/ocenki.php"
    
    @IBOutlet weak var creativity: UILabel!
    @IBOutlet weak var harmony: UILabel!
    @IBOutlet weak var energy: UILabel!
    @IBOutlet weak var choreography: UILabel!
    
    @IBOutlet weak var creativity10: UIButton!
    @IBOutlet weak var creativity09: UIButton!
    @IBOutlet weak var creativity08: UIButton!
    @IBOutlet weak var creativity07: UIButton!
    @IBOutlet weak var creativity06: UIButton!
    @IBOutlet weak var creativity05: UIButton!
    @IBOutlet weak var creativity04: UIButton!
    @IBOutlet weak var creativity03: UIButton!
    @IBOutlet weak var creativity02: UIButton!
    @IBOutlet weak var creativity01: UIButton!
    @IBOutlet weak var creativity00: UIButton!
    
    @IBOutlet weak var harmony10: UIButton!
    @IBOutlet weak var harmony09: UIButton!
    @IBOutlet weak var harmony08: UIButton!
    @IBOutlet weak var harmony07: UIButton!
    @IBOutlet weak var harmony06: UIButton!
    @IBOutlet weak var harmony05: UIButton!
    @IBOutlet weak var harmony04: UIButton!
    @IBOutlet weak var harmony03: UIButton!
    @IBOutlet weak var harmony02: UIButton!
    @IBOutlet weak var harmony01: UIButton!
    @IBOutlet weak var harmony00: UIButton!
    
    @IBOutlet weak var energy10: UIButton!
    @IBOutlet weak var energy09: UIButton!
    @IBOutlet weak var energy08: UIButton!
    @IBOutlet weak var energy07: UIButton!
    @IBOutlet weak var energy06: UIButton!
    @IBOutlet weak var energy05: UIButton!
    @IBOutlet weak var energy04: UIButton!
    @IBOutlet weak var energy03: UIButton!
    @IBOutlet weak var energy02: UIButton!
    @IBOutlet weak var energy01: UIButton!
    @IBOutlet weak var energy00: UIButton!

    @IBOutlet weak var choreography10: UIButton!
    @IBOutlet weak var choreography09: UIButton!
    @IBOutlet weak var choreography08: UIButton!
    @IBOutlet weak var choreography07: UIButton!
    @IBOutlet weak var choreography06: UIButton!
    @IBOutlet weak var choreography05: UIButton!
    @IBOutlet weak var choreography04: UIButton!
    @IBOutlet weak var choreography03: UIButton!
    @IBOutlet weak var choreography02: UIButton!
    @IBOutlet weak var choreography01: UIButton!
    @IBOutlet weak var choreography00: UIButton!
    
    func radioButtonPres1SelectButton(selectedButton: UIButton?) {
        creativity.text = selectedButton?.currentTitle
        pr1 = Int(Double(selectedButton?.currentTitle ?? "0.0")!*10)
        printScoring()
    }
    
    func radioButtonPres2SelectButton(selectedButton: UIButton?) {
        harmony.text = selectedButton?.currentTitle
        pr2 = Int(Double(selectedButton?.currentTitle ?? "0.0")!*10)
        printScoring()
    }
    
    func radioButtonPres3SelectButton(selectedButton: UIButton?) {
        energy.text = selectedButton?.currentTitle
        pr3 = Int(Double(selectedButton?.currentTitle ?? "0.0")!*10)
        printScoring()
    }
    
    func radioButtonPres4SelectButton(selectedButton: UIButton?) {
        choreography.text = selectedButton?.currentTitle
        pr4 = Int(Double(selectedButton?.currentTitle ?? "0.0")!*10)
        printScoring()
    }
    
    func printScoring() {
        
        creativity.text = String(Double(pr1)/Double(10))
        harmony.text = String(Double(pr2)/Double(10))
        energy.text = String(Double(pr3)/Double(10))
        choreography.text = String(Double(pr4)/Double(10))
        let sc = String(Acd)
        let ac = Int(Acd*10)
        accyracy.text = sc
        presentation.text = String(Double(pr1+pr2+pr3+pr4)/Double(10))
        total.text = String(Double(pr1+pr2+pr3+pr4+ac)/Double(10))
        
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        
        radioButtonFreestylePres1 = RadioButtonFreestylePres1(buttons: creativity10, creativity09, creativity08, creativity07, creativity06, creativity05, creativity04, creativity03, creativity02, creativity01, creativity00)
        radioButtonFreestylePres1!.delegate = self
        radioButtonFreestylePres1!.shouldLetDeSelect = true
        
        radioButtonFreestylePres2 = RadioButtonFreestylePres2(buttons: harmony10, harmony09, harmony08, harmony07, harmony06, harmony05, harmony04, harmony03, harmony02, harmony01, harmony00)
        radioButtonFreestylePres2!.delegate = self
        radioButtonFreestylePres2!.shouldLetDeSelect = true
        
        radioButtonFreestylePres3 = RadioButtonFreestylePres3(buttons: energy10, energy09, energy08, energy07, energy06, energy05, energy04, energy03, energy02, energy01, energy00)
        radioButtonFreestylePres3!.delegate = self
        radioButtonFreestylePres3!.shouldLetDeSelect = true
        
        radioButtonFreestylePres4 = RadioButtonFreestylePres4(buttons: choreography10, choreography09, choreography08, choreography07, choreography06, choreography05, choreography04, choreography03, choreography02, choreography01, choreography00)
        radioButtonFreestylePres4!.delegate = self
        radioButtonFreestylePres4!.shouldLetDeSelect = true
        
        printScoring()
    }
    
        func ocenka_send(){
            self.send.isSelected = false
            presentationOc = pr1+pr2+pr3
            let t = Double (accyracy.text!)! * 10
            let p = Double (presentation.text!)! * 10
            let ocenki_t = Int(t)
            let ocenki_p = Int(p)
            let URL_OCENKI = self.idhost + "/php_isap/ocen/ocenka.php"
            let requestURL = NSURL(string: URL_OCENKI)
            //creating NSMutableURLRequest
            let request = NSMutableURLRequest(url: requestURL! as URL)
            //setting the method to post
            request.httpMethod = "POST"
            //creating the post parameter by concatenating the keys and values from text field
            let postParameters = "vistuplenie_idvistuplenie=" + String (self.idvistuplenie!)+"&ocenki_nomsud=" + String (self.sudNum!) + "&ocenki_t=" + String (ocenki_t) + "&ocenki_p=" + String (ocenki_p);
            //adding the parameters to request body
            request.httpBody = postParameters.data(using: String.Encoding.utf8)
            var myJSON = NSDictionary()
            //creating a task to send the post request
            let task = URLSession.shared.dataTask(with: request as URLRequest){
                data, response, error in
                //parsing the response
                do {
                    if (error != nil) {
                        DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1), execute: {
                            self.ocenka_send()
                        })
                    } else {
                    //converting resonse to NSDictionary
                       myJSON  =  try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.allowFragments) as! NSDictionary;
                        self.success = myJSON["success"] as? Int
                    }
                } catch {
                    print(error)
                }
                if (self.success != 0) {
                    DispatchQueue.main.async {
                        let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
                        let nextViewController = storyBoard.instantiateViewController(withIdentifier: "conclusionID") as! ConclusionController
                        nextViewController.Acd = self.Acd
                        nextViewController.idhost = self.idhost
                        nextViewController.idvistuplenie = self.idvistuplenie
                        nextViewController.sudNum = self.sudNum
                        nextViewController.kolSud = self.kolSud
                        nextViewController.presentationOc = self.presentationOc
                        nextViewController.pokaz = self.pokaz
                        nextViewController.sys = self.sys
                        nextViewController.kort = self.kort
                        nextViewController.vistocen = self.vistocen
                        nextViewController.freestyleViv = self.freestyleViv
                        self.navigationController?.pushViewController(nextViewController, animated: true)
                    }
                }
            }
            
            task.resume()
            
        }
        
        @IBAction func send(_ sender: UIButton) {
            ocenka_send()
        }
    
}

